/*
 * @Description: 用户信息 store
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-10-12
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-04-24
 */

import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

import { UserSexEnum } from '@/api/system/ysys-user/user/enum'

/** 用户信息全局状态 */
export const useUserInfoStore = defineStore('userInfo', () => {
  //idCard: null, idType: 1, nickName: null, userInfoId: "12128"

  /** 用户id */
  const userId = ref('')
  /** 用户名 */
  const userName = ref('')
  /** 用户性别 */
  const userSex = ref(UserSexEnum.MALE)
  /** 头像 */
  const headImg = ref('')
  /** 手机号 */
  const phone = ref('')

  /**
   * 设置用户信息
   * @param _userInfo 用户信息
   */
  const setUserInfo = (_userInfo: IUserInfo) => {
    const { userId, userName, headImg, userSex, phone } = _userInfo
    setUserId(userId)
    setUserName(userName)
    setUserSex(userSex)
    setHeadImg(headImg)
    setPhone(phone)
  }

  /**
   * 设置用户id
   * @param _userId 用户性别
   */
  const setUserId = (_userId: string) => (userId.value = _userId)

  /**
   * 设置用户名
   * @param _userName 用户名
   */
  const setUserName = (_userName: string) => (userName.value = _userName)

  /**
   * 设置用户性别
   * @param _userSex 用户性别
   */
  const setUserSex = (_userSex: number) => (userSex.value = _userSex)

  /**
   * 设置头像
   * @param _headImg 头像
   */
  const setHeadImg = (_headImg: string) => (headImg.value = _headImg)

  /**
   * 设置手机号
   * @param _phone 手机号
   */
  const setPhone = (_phone: string) => (phone.value = _phone)

  /** 是否绑定手机号 */
  const isPhoneBind = computed(() => !!phone.value)

  /** 检测类型 */
  const detectionType = ref()
  /**
   * 设置检测类型
   */
  const setDetectionType = (type: number) => (detectionType.value = type)

  return { userId, userName, userSex, headImg, phone, setUserInfo, setUserId, setUserName, setUserSex, setHeadImg, setPhone, isPhoneBind, detectionType }
})
